﻿<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
include('../class.base.php');
if (!$sess = sisse_logitud()) { header("Location: ../index.php"); die(); }
if ($grupi_oigused = grupi_oigused($sess)) {
	if ($grupi_oigused < 2) {
		header("Location: index.php"); die();
	}
}
else {
	header("Location: index.php"); die();
}
if (isset($_GET["tabel"])) { //tahetkse saada lisamise vormi
		$public_tables = getPublicTables();
        $xtabel = htmlspecialchars($_GET["tabel"], ENT_QUOTES);
		if (isset($_GET["u"])) {
			$u = htmlspecialchars($_GET["u"], ENT_QUOTES);
			$hide = htmlspecialchars($_GET["hide"], ENT_QUOTES);
		}
		else {
			$u = "";
			$hide = "0";
		}
		if (!in_array($xtabel, getNonSystemTables())) { die("<center><img src='kujundus/pildid/viga.png'><br />".$lang["tabelit"]." ".$xtabel." ".$lang["ei_leitud"]."</center>"); }
		
				$xtabel1 = kriips($xtabel, 0);
				$pealkiri1 = $lang["lisa"]." ".$xtabel1;
				$sisu = "";
				$tabelid = getNonSystemTables();
				$ylemad = mysql_query("SHOW COLUMNS FROM ".$xtabel);
				while ($rida = mysql_fetch_array($ylemad)) {
					if (in_array($rida[0], $tabelid)) {
						$sisu .= "<span id=\"".$rida[0]."_".$xtabel."_sisu\"></span>";
					}
				}
				$sisu .="
				<form id='".$xtabel.$u."' action='#'>
				<table width='100%'>";
				$result = mysql_query("SHOW COLUMNS FROM ".$xtabel);
				$excludes = excludes(0);
				if (in_array($xtabel, getInfoTables())) { unset($excludes[0]); }
				while ($columns = mysql_fetch_array($result)) { //genereeritakse html vorm
					if (!in_array($columns[0], $excludes)) {
						$leiatyyp = explode("(", $columns[1]);
						$tyyp = $leiatyyp[0];
						if ($columns[2] == "NO") { $tarn = "*"; } else { $tarn = ""; }
						$sisu .= "<tr><td>".kriips($columns[0])." ".$tarn."</td><td>";
						if (in_array($columns[0], $tabelid)) {
							if ($_GET["id"]) {
								if ($_GET["u"] == "osaleja") {
									$_GET["id"] = htmlspecialchars($_GET["id"], ENT_QUOTES);
									$whereId = " WHERE id='".$_GET["id"]."'";
									$selected = $_GET["id"];
								}
								else {
									$whereId = "";
									if ($tyyp == "int") {
										$maxIdQuery = mysql_fetch_array(mysql_query("SELECT MAX(id) AS id FROM ".$columns[0]));
										$selected = $maxIdQuery["id"];
									}
								}
							}
							else {
								$whereId = "";
								if ($tyyp == "int") {
									$maxIdQuery = mysql_fetch_array(mysql_query("SELECT MAX(id) AS id FROM ".$columns[0]));
									$selected = $maxIdQuery["id"];
								}
							}
							if ($columns[0] == "rolli_nimetus") { $selected = "Keelejuht"; }
							$shows = get_must_show_columns($columns[0]);
							$kysi_teisest_tabelist = mysql_query("SELECT * FROM ".$columns[0].$whereId." ORDER BY ".$shows[0]);
							if ($tyyp == "int") { $onChange = "onchange=\"arhiiviNr('".$xtabel."', this.value);"; } else { $onChange = ""; }
							$sisu .= "<span id=\"uus_".$xtabel."_".$columns[0]."\"><select name='".$columns[0]."' width='300' style='width: 300px' ".$onChange." ab('".$xtabel.$u."');\"><option value=''></option>";
							while ($rida = mysql_fetch_array($kysi_teisest_tabelist)) {
								if ($rida["id"] == $selected) { $selectedF = " selected='selected'"; } else { $selectedF = ""; }
								if ($columns[0] != "sari" || $rida["materjali_tyyp"] == $xtabel || $rida["materjali_tyyp"] == $xtabel."_trakk" || $rida["materjali_tyyp"]."album" == $xtabel) {
									$sisu .= "
										<option value='".$rida["id"]."'".$selectedF.">";
									foreach ($shows as $show) { $sisu .= " ".$rida[$show]; }
									$sisu .= "</option>";
								}
							}
							$sisu .= "</select></span>";
							$sisu .= "<input type=\"submit\" name=\"lisanupp\" value=\"Lisa ".kriips($columns[0], 0)."\" onclick=\"lisa_vorm('".$columns[0]."', '".$xtabel."', '1'); this.disabled = true; document.getElementById('hide_".$columns[0]."_".$xtabel."').style.display='inline'; return false;\">
								<img id=\"hide_".$columns[0]."_".$xtabel."\" src=\"kujundus/pildid/hide.png\" style=\"display: none;\" alt=\"Peida ".kriips($columns[0], 0)." lisamisvorm\" title=\"Peida ".kriips($columns[0], 0)." lisamisvorm\" onclick=\"document.getElementById('".$columns[0]."_".$xtabel."_sisu').style.display='none'; this.style.display='none';\">";
						}
						elseif ($columns[0] == "ressursi_tyyp") {
							$sisu .= "
							<select name='ressursi_tyyp' onchange=\"ab('".$xtabel.$u."');\">
								<option value=''></option>
								<option value='1'>".$lang["fyysiline"]."</option>
								<option value='2'>".$lang["digitaalne"]."</option>
							</select>";
						}
						elseif ($columns[0] == "fyysiliselt_olemas") {
							$sisu .= "
							<select name='fyysiliselt_olemas' onchange=\"ab('".$xtabel.$u."');\">
								<option value=''></option>
								<option value='1'>".$lang["olemas"]."</option>
								<option value='2'>".$lang["kadunud"]."</option>
								<option value='3'>".$lang["laenatud"]."</option>
							</select>";
						}
						elseif ($columns[0] == "digitaalselt_olemas") {
							$sisu .= "
							<select name='digitaalselt_olemas' onchange=\"ab('".$xtabel.$u."');\">
								<option value=''></option>
								<option value='1'>".$lang["jah"]."</option>
								<option value='2'>".$lang["ei"]."</option>
							</select>";
						}
						elseif ($columns[0] == "oigused") {
							$sisu .= "
								<select name='oigused' onchange=\"ab('".$xtabel.$u."');\">
									<option value='avalik'>".$lang["avalik"]."</option>
									<option value='mitteavalik' selected='selected'>".$lang["mitteavalik"]."</option>
								</select>";
						}
						elseif ($columns[0] == "materjal" and isset($_GET["u"]) and $_GET["u"] != "osaleja") {
							$sisu .= kriips($_GET["u"])."<input type='hidden' name='materjal' value=\"".$_GET["u"]."\">";
						}
						elseif ($columns[0] == "materjal") {
							$sisu .= "
								<select name='materjal' onchange=\"generate_list(this.value, '".$xtabel."'); ab('".$xtabel.$u."');\">
									<option value=''></option>";
							foreach ($public_tables as $value) {
								$sisu .= "
									<option value='".$value."'>".kriips($value)."</option>";
							}
							$sisu .= "</select>";
						}
						elseif ($columns[0] == "materjal_id" and isset($_GET["u"]) and $_GET["u"] != "osaleja") {
							$teine_tabel = htmlspecialchars($_GET["u"], ENT_QUOTES);
							if (isset($_GET["id"])) {
								$_GET["id"] = htmlspecialchars($_GET["id"], ENT_QUOTES);
								$whereId = " WHERE id='".$_GET["id"]."'";
							}
							else {
								$whereId = "";
							}
							$must_have_columns = get_must_show_columns($teine_tabel);
							$paring = mysql_query("SELECT * FROM ".$teine_tabel.$whereId." ORDER BY ".$must_have_columns[0]);
							$maxIdQuery = mysql_fetch_array(mysql_query("SELECT MAX(id) AS id FROM ".$teine_tabel));
							$selected = $maxIdQuery["id"];
							$sisu .= "<span id='uus_".$xtabel."_".$xtabel."'><select name='materjal_id' onchange=\"ab('".$xtabel.$u."');\">";
							while ($rida = mysql_fetch_array($paring)) {
								if ($rida["id"] == $selected) { $selectedF = " selected='selected'"; } else { $selectedF = ""; }
								$sisu .= "
								<option value='".$rida["id"]."'".$selectedF.">";
								foreach ($must_have_columns as $value) {
									if ($value == "kestus") {
										$sisu .= pretty_length($rida[$value])." ";
									}
									else {
										$sisu .= $rida[$value]." ";
									}
								}
								$sisu .= "</option>";
							}
							$sisu .= "</select></span>";
						}
						elseif ($columns[0] == "materjal_id") {
							$sisu .= "<span id='uus_".$xtabel."_".$xtabel."'></span>";
						}
						elseif ($columns[0] == "kestus") {
							$sisu .= "<input type='text' name='kestus' onchange=\"ab('".$xtabel.$u."');\"> ".$lang["numbrid"]." ".$lang["TT_MM_SS"];
						}
						elseif ($columns[0] == "arhiivinumber") {
							if ($xtabel == "audio") { $up = "sari"; $digits = 4; $sep = ""; }
							elseif ($xtabel == "audio_trakk") { $up = "audio"; $digits = 2; $sep = "-"; }
							elseif ($xtabel == "foto") { $up = "fotoalbum"; $digits = 3; $sep = "-"; }
							elseif ($xtabel == "fotoalbum") { $up = "sari"; $digits = 4; $sep = ""; }
							elseif ($xtabel == "kasikiri") { $up = "sari"; $digits = 4; $sep = ""; }
							elseif ($xtabel == "video") { $up = "sari"; $digits = 4; $sep = ""; }
							elseif ($xtabel == "video_trakk") { $up = "video"; $digits = 2; $sep = "-"; }
							else { $arhiiviNumber = ""; }
							if (isset($up)) {
								if ($up == "sari") {
									$what = "sarja_tahis";
									$whereMaterjal = " WHERE materjali_tyyp LIKE '".$xtabel."%' OR materjali_tyyp LIKE '".str_replace("album", "", $xtabel)."%'";
								}
								else {
									$what = "arhiivinumber";
									$whereMaterjal = "";
								}
								$query = mysql_query("SELECT id,".$what." FROM ".$up.$whereMaterjal." ORDER BY id DESC LIMIT 1");
								if (mysql_num_rows($query)) {
									$row = mysql_fetch_array($query);
									$arhiiviNumber = $row[$what];
									$start = mysql_query("SELECT * FROM ".$xtabel." WHERE ".$up."=".$row["id"]." ORDER BY arhiivinumber DESC LIMIT 1");
									if (mysql_num_rows($start)) {
										$rida = mysql_fetch_array($start);
										$lastNrParts = explode($arhiiviNumber, $rida["arhiivinumber"]);
										if ($sep == "-") {
											$linesep = explode("-", $lastNrParts[1]);
											$lastNrParts[1] = $linesep[1];
										}
										$newNr = $lastNrParts[1] + 1;
									}
									else {
										$newNr = 1;
									}
									while (strlen($newNr) < $digits) {
										$newNr = "0".$newNr;
									}
									$arhiiviNumber = $arhiiviNumber.$sep.$newNr;
								}
								else {
									$arhiiviNumber = "";
								}
							}
							$sisu .= "<span id='".$xtabel."_arhiiv'><input type='text' name='arhiivinumber' onchange=\"ab('".$xtabel.$u."');\" value=\"".$arhiiviNumber."\"></span>".pretty_filetype($tyyp);
						}
						elseif ($tyyp == "text") { $sisu .= "<textarea name='".$columns[0]."' cols='25' rows='3' onchange=\"ab('".$xtabel.$u."');\"></textarea>".pretty_filetype($tyyp); }
						elseif ($columns[0] == "materjali_tyyp") {
							$sisu .= "
								<select name='materjali_tyyp' onchange=\"ab('".$xtabel.$u."');\">";
							foreach ($public_tables as $public_table) {
								$sisu .= "<option value='".$public_table."'>".kriips($public_table)."</option>";
							}
							$sisu .= "</select>";
						}
						else { $sisu .= "<input type='text' name='".$columns[0]."' onchange=\"ab('".$xtabel.$u."');\">".pretty_filetype($tyyp); }
						$sisu .= "</td></tr>";
					}
				} //genereeriti html vorm
				$sisu .="
				<tr><td></td><td><input type='submit' id='lisaButton_".$xtabel."' value='".$lang["lisa"]." ".$xtabel1."' onclick=\"lisa('".$xtabel."', '".$u."', '".$hide."'); return false;\" /></td></tr>
				</table>
				</form>";
				foreach ($tabelid as $tabel) {
					$alamad = mysql_query("SHOW COLUMNS FROM ".$tabel);
					while ($rida = mysql_fetch_array($alamad)) {
						if ($rida[0] == $xtabel) {
							$sisu .= "<input type=\"submit\" id=\"lisa_".$tabel."\" style=\"display:none;\" value=\"".$lang["lisa"]." ".kriips($tabel, 0)."\" onclick=\"lisa_vorm('".$tabel."', '".$xtabel."', '0'); this.disabled = true; document.getElementById('hide_".$tabel."_".$xtabel."').style.display='inline'; return false;\">
								<img id=\"hide_".$tabel."_".$xtabel."\" src=\"kujundus/pildid/hide.png\" style=\"display: none;\" alt=\"".$lang["peida"]." ".kriips($tabel, 0)." lisamisvorm\" title=\"".$lang["peida"]." ".kriips($tabel, 0)." lisamisvorm\" onclick=\"document.getElementById('".$tabel."_".$xtabel."_sisu').style.display='none'; document.getElementById('lisa_".$tabel."').disabled = false; this.style.display='none';\">
							<br />";
							$sisu .= "<span id=\"".$tabel."_".$xtabel."_sisu\"></span>";
						}
					}
				}
				
				if (in_array($xtabel, $public_tables)) {
					$sisu .= "<input type=\"submit\" id=\"lisa_digitaliseerija\" style=\"display:none;\" value=\"Lisa digitaliseerija\" onclick=\"lisa_vorm('digitaliseerija', '".$xtabel."', '0'); this.disabled = true; document.getElementById('hide_digitaliseerija_".$xtabel."').style.display='inline'; return false;\">
						<img id=\"hide_digitaliseerija_".$xtabel."\" src=\"kujundus/pildid/hide.png\" style=\"display: none;\" alt=\"".$lang["peida"]." ".$lang["digitaliseerija_lisamisvorm"]."\" title=\"".$lang["peida"]." ".$lang["digitaliseerija_lisamisvorm"]."\" onclick=\"document.getElementById('digitaliseerija_".$xtabel."_sisu').style.display='none'; document.getElementById('lisa_digitaliseerija').disabled = false; this.style.display='none';\">
					<br />";
					$sisu .= "<span id=\"digitaliseerija_".$xtabel."_sisu\"></span>";
					
					$sisu .= "<input type=\"submit\" id=\"lisa_roll\" style=\"display:none;\" value=\"Lisa mingi muu roll\" onclick=\"lisa_vorm('roll', '".$xtabel."', '0'); this.disabled = true; document.getElementById('hide_roll_".$xtabel."').style.display='inline'; return false;\">
						<img id=\"hide_roll_".$xtabel."\" src=\"kujundus/pildid/hide.png\" style=\"display: none;\" alt=\"".$lang["peida"]." ".$lang["rolli_lisamisvorm"]."\" title=\"".$lang["peida"]." ".$lang["rolli_lisamisvorm"]."\" onclick=\"document.getElementById('roll_".$xtabel."_sisu').style.display='none'; document.getElementById('lisa_roll').disabled = false; this.style.display='none';\">
					<br />";
					$sisu .= "<span id=\"roll_".$xtabel."_sisu\"></span>";
				}
echo $sisu;
} //taheti saada lisamise vormi
?>